<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生信息管理系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1000px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #4CAF50;
            color: white;
            font-weight: bold;
        }
        tr:hover {
            background-color: #f5f5f5;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .no-data {
            text-align: center;
            color: #666;
            font-style: italic;
            padding: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        .form-group input, .form-group select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .btn {
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 5px;
        }
        .btn-primary {
            background-color: #4CAF50;
            color: white;
        }
        .btn-danger {
            background-color: #f44336;
            color: white;
        }
        .btn-warning {
            background-color: #ff9800;
            color: white;
        }
        .form-container {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .actions {
            display: flex;
            gap: 5px;
        }
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgb(0,0,0);
            background-color: rgba(0,0,0,0.4);
        }
        .modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 50%;
        }
        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }
        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }
        .note {
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            border-radius: 4px;
            padding: 10px;
            margin-bottom: 15px;
            color: #856404;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>学生信息管理系统</h1>
    
    <div class="note">
        <strong>提示:</strong> ID字段现在可以手动填写，如果不填写系统将自动生成唯一ID。
    </div>
    
    <!-- 添加/编辑表单 -->
    <div class="form-container">
        <h2 id="formTitle">添加学生</h2>
        <form id="studentForm">
            <div class="form-group">
                <label for="studentId">ID (可选，不填则自动生成):</label>
                <input type="number" id="studentId">
            </div>
            <div class="form-group">
                <label for="name">姓名:</label>
                <input type="text" id="name" required>
            </div>
            <div class="form-group">
                <label for="age">年龄:</label>
                <input type="number" id="age" required>
            </div>
            <div class="form-group">
                <label for="gender">性别:</label>
                <select id="gender" required>
                    <option value="">请选择</option>
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </div>
            <button type="submit" class="btn btn-primary" id="saveBtn">保存</button>
            <button type="button" class="btn" id="cancelBtn">取消</button>
        </form>
    </div>
    
    <!-- 学生列表 -->
    <h2>学生列表</h2>
    <table id="studentTable">
        <thead>
        <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="studentList">
        <!-- 数据将通过JavaScript动态插入 -->
        </tbody>
    </table>
</div>

<script>
    // 页面加载完成后获取学生列表
    document.addEventListener('DOMContentLoaded', function() {
        loadStudentList();
        
        // 绑定表单提交事件
        document.getElementById('studentForm').addEventListener('submit', saveStudent);
        
        // 绑定取消按钮事件
        document.getElementById('cancelBtn').addEventListener('click', resetForm);
    });
    
    // 获取学生列表
    function loadStudentList() {
        fetch('/api/list')
            .then(response => response.json())
            .then(data => {
                if (data.code === 1) {
                    renderStudents(data.data);
                } else {
                    console.error('获取数据失败:', data.msg);
                    alert('获取数据失败: ' + data.msg);
                }
            })
            .catch(error => {
                console.error('请求出错:', error);
                alert('请求出错: ' + error);
            });
    }
    
    // 渲染学生数据
    function renderStudents(students) {
        const tbody = document.getElementById('studentList');
        
        if (!students || students.length === 0) {
            tbody.innerHTML = '<tr><td colspan="5" class="no-data">暂无学生数据</td></tr>';
            return;
        }
        
        const studentRows = students.map(student => `
            <tr>
                <td>${student.id}</td>
                <td>${student.name}</td>
                <td>${student.age}</td>
                <td>${student.gender}</td>
                <td class="actions">
                    <button class="btn btn-warning" onclick="editStudent(${student.id})">编辑</button>
                    <button class="btn btn-danger" onclick="deleteStudent(${student.id})">删除</button>
                </td>
            </tr>
        `).join('');
        
        tbody.innerHTML = studentRows;
    }
    
    // 保存学生（添加或更新）
    function saveStudent(event) {
        event.preventDefault();
        
        const studentId = document.getElementById('studentId').value;
        const student = {
            name: document.getElementById('name').value,
            age: parseInt(document.getElementById('age').value),
            gender: document.getElementById('gender').value
        };
        
        // 如果ID字段有值，则设置ID
        if (studentId) {
            student.id = parseInt(studentId);
        }
        
        const url = studentId ? `/api/student` : `/api/student`;
        const method = studentId ? 'PUT' : 'POST';
        
        if (studentId) {
            student.id = parseInt(studentId);
        }
        
        fetch(url, {
            method: method,
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(student)
        })
        .then(response => response.json())
        .then(data => {
            if (data.code === 1) {
                alert(studentId ? '更新成功' : '添加成功');
                resetForm();
                loadStudentList();
            } else {
                alert(studentId ? '更新失败: ' + data.msg : '添加失败: ' + data.msg);
            }
        })
        .catch(error => {
            console.error('保存失败:', error);
            alert('保存失败: ' + error);
        });
    }
    
    // 编辑学生
    function editStudent(id) {
        fetch(`/api/student/${id}`)
            .then(response => response.json())
            .then(data => {
                if (data.code === 1) {
                    const student = data.data;
                    document.getElementById('studentId').value = student.id;
                    document.getElementById('name').value = student.name;
                    document.getElementById('age').value = student.age;
                    document.getElementById('gender').value = student.gender;
                    document.getElementById('formTitle').textContent = '编辑学生';
                } else {
                    alert('获取学生信息失败: ' + data.msg);
                }
            })
            .catch(error => {
                console.error('获取学生信息失败:', error);
                alert('获取学生信息失败: ' + error);
            });
    }
    
    // 删除学生
    function deleteStudent(id) {
        if (confirm('确定要删除这个学生吗？')) {
            fetch(`/api/student/${id}`, {
                method: 'DELETE'
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 1) {
                    alert('删除成功');
                    loadStudentList();
                } else {
                    alert('删除失败: ' + data.msg);
                }
            })
            .catch(error => {
                console.error('删除失败:', error);
                alert('删除失败: ' + error);
            });
        }
    }
    
    // 重置表单
    function resetForm() {
        document.getElementById('studentForm').reset();
        document.getElementById('formTitle').textContent = '添加学生';
    }
</script>
</body>
</html>